package com.usian.wemedia.controller;

import com.usian.api.wemedia.WmNewsApi;
import com.usian.model.admin.dtos.NewsAuthDto;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmNewsDto;
import com.usian.model.media.dtos.WmNewsPageReqDto;
import com.usian.model.media.pojos.WmNews;
import com.usian.model.media.pojos.WmNewsVo;
import com.usian.model.media.pojos.WmUser;
import com.usian.wemedia.service.WmNewsService;
import com.usian.wemedia.service.WmUserLoginService;
import com.usian.wemedia.service.WmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController implements WmNewsApi {

    @Autowired
    WmNewsService wmNewsService;

    @Autowired
    WmUserService wmUserService;

    @Autowired
    WmUserLoginService wmUserLoginService;

    /**
     * 内容列表查询
     * @param dto
     * @return
     */
    @PostMapping("/list")
    @Override
    public ResponseResult findPage(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.findPage(dto);
    }

    /**
     * 提交文章
     * @param dto
     * @return
     */
    @PostMapping("/submit")
    @Override
    public ResponseResult submitNews(@RequestBody WmNewsDto dto) {
        if (dto.getStatus()==1){
            return wmNewsService.submitNews(dto,(short)1);
        }else {
            return wmNewsService.submitNews(dto,(short)0);
        }
    }

    /**
     * 根据id查询修改的数据，返回修改数据
     * @param id
     * @return
     */
    @GetMapping("/one/{id}")
    @Override
    public ResponseResult findByNewId(@PathVariable("id") Integer id) {
        return wmNewsService.findByNewId(id);
    }

    /**
     * 删除(已上架不能删)
     * @param id
     * @return
     */
    @GetMapping("/del_news/{id}")
    @Override
    public ResponseResult delByNewId(@PathVariable("id") Integer id) {
        return wmNewsService.delByNewId(id);
    }

    /**
     * 文章上下架
     * @param dto
     * @return
     */
    @PostMapping("/down_or_up")
    @Override
    public ResponseResult downOrUp(@RequestBody WmNewsDto dto) {
        return wmNewsService.downOrUp(dto);
    }


    /**
     * admin远程调用根据id查询文章信息
     * @param id
     * @return
     */
    @GetMapping("/ones/{id}")
    @Override
    public WmNews findByNewsId(@PathVariable("id") Integer id) {
        return wmNewsService.getById(id);
    }


    /**
     * admin远程调用修改文章状态
     * @param wmNews
     * @return
     */
    @PostMapping("/updateByStatus")
    @Override
    public ResponseResult updateByStatus(@RequestBody WmNews wmNews) {
        return wmNewsService.updateByStatus(wmNews);
    }


    /**
     * admin远程调用根据userid查询当前用户的信息
     * @param userId
     * @return
     */
    @GetMapping("/findByUserId/{id}")
    @Override
    public WmUser findByUserId(@PathVariable("id") Integer userId) {
        return wmUserService.findByUserId(userId);
    }

    @GetMapping("/findByWmUserId/{id}")
    @Override
    public WmUser findByWmUserId(@PathVariable("id") Integer userId) {
        return wmUserService.getById(userId);
    }

    @GetMapping("/findRelease")
    @Override
    public List<Integer> findNews() {
        return wmNewsService.findNews();
    }

    /**
     * 查询人工审核的列表
     * @param dto
     * @return
     */
    @PostMapping("/findList")
    @Override
    public ResponseResult findList(@RequestBody NewsAuthDto dto) {
        return wmNewsService.findList(dto);
    }


    /**
     *  查询文章详情
     * @param id
     * @return
     */
    @GetMapping("/findNewById/{id}")
    @Override
    public WmNewsVo findWmNewsVo(@PathVariable("id") Integer id) {
        return wmNewsService.findWmNewsVo(id);
    }
}
